package person.bangbang.im.Androidgin.Framework;

import java.util.ArrayList;
import java.util.List;

public class Chat {
	/* with which u talk */
	private Buddy mPartner;
	private String mTopic;
	
	/* notify id for notifying user a message event when she(he) is out */
	private int mNotifyId;
	
	/* chat message history */
	private List<Message> mMessages = new ArrayList<Message>();
	
	private List<OnImEvent> mImEventListeners = new ArrayList<OnImEvent>();

	public Chat(Buddy parnter, int id) {
		mPartner = parnter ;
		mTopic = "";
		mNotifyId = id;
	}
	public Chat(Buddy parnter, String topic, int id) {
		mPartner = parnter;
		mTopic = topic;
		mNotifyId = id;
	}
	protected void addIMessage(Message im) {
		mMessages.add(im);
	}

	public Buddy getPartner() {
		return mPartner;
	}

	public List<Message> getIMessages() {
		return mMessages;
	}

	public String getTopic() {
		return mTopic;
	}
	
	public int getNotifyId() {
		return mNotifyId;
	}


	public void regOnImEventListener(OnImEvent l) {
		mImEventListeners.add(l);
	}
	public void unRegOnImEventListener(OnImEvent l) {
		mImEventListeners.remove(l);
	}
	public void onImSend(Message e) {
		addIMessage(e);
		
		for (OnImEvent event : mImEventListeners) {
			event.onSndIm(e);
		}
	}
	public void onImRcvd(Message m) {
		addIMessage(m);
		
		for (OnImEvent e : mImEventListeners) {
			e.onRcvdIm(m);
		}
	}
	public void onImErr(int code, String detail) {
		for (OnImEvent e : mImEventListeners) {
			e.onErrIm(code, detail);
		}
	}
	public interface OnImEvent {
		// received a IM
		public void onRcvdIm(Message msg);
		// a IM has been sent. arrive??? i don't know.
		public void onSndIm(Message msg);
		// something wrong occur.
		public void onErrIm(int code, String detail);
	}
	
}
